<template>
  <view class="orderDetail">
    <view class="orderItem">
      <view class="topStatus" v-if="payDetail">
        <view class="date">订单状态</view>
        <view class="status" v-if="orderItemObj.o_is_refund == -1">
          {{orderItemObj.o_status == -1 ? '已取消' : orderItemObj.o_status == 0 ? '待支付' : orderItemObj.o_status == 1 ? '待取镜' : orderItemObj.o_status == 2 ? '已取镜' : orderItemObj.o_status == 3 ? '已完成' : ''}}
        </view>
        <view v-else class="status">
          {{orderItemObj.o_status == -1 ? '已取消' : orderItemObj.o_status == 0 ? '待支付' : orderItemObj.o_status == 1 ? '待取镜' : orderItemObj.o_status == 2 ? '已取镜' : orderItemObj.o_status == 3 ? '已完成' : ''}}
        </view>
      </view>
      <view class="middleContent">
        <view class="middleItem"><text style="font-weight: 500;">订单号</text><text>{{orderItemObj.o_order_code}}</text></view>
        <view class="middleItem"><text style="font-weight: 500;">租借数量</text><text>{{orderItemObj.o_glasses_num}}副</text></view>
        <view class="middleItem"><text style="font-weight: 500;">押金金额</text><text>¥{{ orderItemObj.o_money }}</text></view>
      </view>
      <view class="onShowHideContent">
        <view class="middleItem" v-for="glass in orderItemObj.o_content" :key="glass.t_code">
          <view class="imageBox">
            <view class="glassImg">
              <image :src="glass.t_type_img"></image>
            </view>
            <text>{{glass.t_type_name + ' x'+ glass.num }}</text>
          </view>
          <text>¥{{ glass.t_deposit * glass.num  }}</text>
        </view>
      </view>
      <view class="middleContent_pay" v-if="o_status != 0">
        <view class="middleItem" v-if="orderItemObj.o_pay_code"><text style="font-weight: 500;">支付编码:</text><text>{{orderItemObj.o_pay_code}}</text></view>
        <view class="middleItem"  v-if="orderItemObj.o_pay_time"><text style="font-weight: 500;">付款时间:</text><text>{{ orderItemObj.o_pay_time }}</text></view>
        <view class="middleItem" v-if="orderItemObj.o_area"><text style="font-weight: 500;">取镜地址:</text><text>{{ orderItemObj.o_area }}</text></view>
        <view class="middleItem"  v-if="orderItemObj.f_num"><text style="font-weight: 500;">已还数量:</text><text>{{orderItemObj.f_num}}副</text></view>
        <view class="middleItem"  v-if="orderItemObj.num"><text style="font-weight: 500;">已取数量:</text><text>{{orderItemObj.num}}副</text></view>
        <view class="middleItem"  v-if="orderItemObj.o_is_refund"><text style="font-weight: 500;">是否退款:</text><text>{{ orderItemObj.o_is_refund == -1 ? '已退款' : orderItemObj.o_is_refund == 1 ? '退款中' : '未退款' }}</text></view>
        <view class="middleItem"  v-if="orderItemObj.refund_fee"><text style="font-weight: 500;">退款金额:</text><text>¥{{ orderItemObj.refund_fee }}</text></view>
        <view class="middleItem" v-if="orderItemObj.refund_time"><text style="font-weight: 500;">退款时间:</text><text>{{ orderItemObj.refund_time }}</text></view>

      </view>
      <view class="orderTime" v-if="payDetail">
        <view class="middleItem"><text style="font-weight: 500;">下单时间</text><text>{{ orderItemObj.o_start_time }}</text></view>
      </view>
    </view>
    <view class="instrotion" v-if="orderItemObj.o_status == 0">
      <view class="title">说明：</view>
      <view class="title1">1.眼镜出货款式随机，以实物为准。</view>
      <view class="title1"> 2.支付押金是为了避免眼镜损坏、丢失等不良行为。</view>
      <view class="title1">3.归还时间：租赁眼镜使用完毕后，请就近及时归还至取镜机，如超过72小时未归还，重影生活有权扣除对应眼镜部分或全部押金作为赔偿。</view>
      <view class="title1">4.归还条件：租赁眼镜应保持原状，不得有损坏等。</view>
      <view class="title1">5.眼镜丢失或者损坏，扣除对应眼镜押金。</view>
      <view class="title1">6.退款：租赁眼镜归还后，重影生活会立即办理退款手续，押金将退还至用户小程序账户，敬请您及时办理退款手续，但退款到账时间会根据您选择的支付方式所对应的银行或者第三方支付的处理流程不同有所差别:通常情况下，退款到账需要0-72小时。</view>
    </view>
    <view class="btnCOntainer">
      <AtButton
        v-if="orderItemObj.o_status == 0"
        type="primary"
        @click="payOrder"
        :loading="isOpened">
        支付{{ orderItemObj.o_real_money }}元
      </AtButton>
      <AtButton v-if="orderItemObj.o_status == 1" type="primary" @click="gotoGetGlass">去取镜</AtButton>
    </view>
    <AtToast :isOpened="isOpened" text="支付中, 请稍等" status="loading" :duration="0" :hasMask="true"></AtToast>

  </view>
</template>

<script setup>
import './index.scss'
import { AtButton,AtToast } from 'taro-ui-vue3'
import {queryOrderInfo, getUnifiedorder, return_order_glasses_info, create_wx_config} from '../../http/api'
import Taro, {useDidShow} from '@tarojs/taro'
import {ref} from 'vue'
const orderItemObj = ref({})
const isOpened = ref(false)
const payDetail = ref(true)
const UStatus = ref(0)
function gotoGetGlass () {
  Taro.getSetting({
    withSubscriptions: true,
    success: (res) => {
      if (!res.subscriptionsSetting.mainSwitch
      || (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings['8E5tUziPb5oq4Q06XvXtPGL4eyPV0g64KQ6oRKkguLI']) == 'reject'
    ) {
        Taro.showModal({
          title: '温馨提示',
          showCancel: false,
          content: '为了您能接收到退款提醒消息，请打开设置勾选订阅接受通知消息',
          success: function (res) {
            if (res.confirm) {
              Taro.openSetting({
                success: function (res) {
                  console.log('授权成功回来', res)
                  Taro.showModal({title: '温馨提示',
                    content: '订阅消息通知，方便给您退款通知',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        Taro.requestSubscribeMessage({
                          tmplIds: ['8E5tUziPb5oq4Q06XvXtPGL4eyPV0g64KQ6oRKkguLI'],
                          complete: function (Subscribe) {
                          }
                        })
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }})

                }
              })
            } else if (res.cancel) {
              //  gotoGetGlass()
            }
          }
        })
      } else {
        Taro.redirectTo({
          url: `/pages/QRCode/index?o_order_code=${orderItemObj.value.o_order_code}`
        })
      }
    }
  })
}
function payOrder() {
  isOpened.value = true
  getUnifiedorder({o_order_code: orderItemObj.value.o_order_code}).then(res => {
    isOpened.value = false
    if (res.code === '1000') {
      let payData = res.data
      Taro.requestPayment({
        timeStamp: payData.timeStamp,
        nonceStr: payData.nonceStr,
        package: payData.package,
        signType: payData.signType,
        paySign: payData.paySign,
        success: function (payRes) {
          // Taro.setNavigationBarTitle({
          //     title: '取镜信息'
          // })
          // getOrderInfo(paramsData.value)
          Taro.requestSubscribeMessage({
              tmplIds: ['8E5tUziPb5oq4Q06XvXtPGL4eyPV0g64KQ6oRKkguLI'],
              complete: function (Subscribe) {
                Taro.setNavigationBarTitle({
                  title: '取镜信息'
                })
                getOrderInfo(paramsData.value)
              }
            })
        },
        fail: function (res) {

        }
      })
    } else {
      // gotoGetGlass()
      isOpened.value = false
      Taro.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
    }
  }).catch(() => {
      // gotoGetGlass()
      isOpened.value = false
  })
}
const paramsData = ref({})
const getOrderInfo = (params) => {
  queryOrderInfo({o_order_code: params.o_order_code }).then(res => {
    if (res.code == '1000') {
      orderItemObj.value = res.data;
    }
  })
}
useDidShow(() => {
  const wx_status = Taro.getStorageSync('wx_status')
  if (wx_status){
    UStatus.value = wx_status || 0
  }
  let params =Taro.getCurrentInstance().router.params
  paramsData.value = params
  if (params.from == 'index' || params.from == 'login') {
    payDetail.value = false
    Taro.setNavigationBarTitle({
      title: '支付信息'
    })
  }
  if (params.o_is_refund == -1) {
    return_order_glasses_info({o_order_code: params.o_order_code }).then(res => {
      if (res.code == '1000') {
        orderItemObj.value = res.data;
      }
    })
  } else {
    getOrderInfo(params)

  }

})

Taro.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})


</script>
